home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_d / sndkey11.zip / SMAIN.PAS < prev    next >
Pascal/Delphi Source File  |  1996-04-01  |  5KB  |  112 lines

  1. unit Smain;
  2.  
  3. { Example program demontrating the use of SNDKEYxx.DLL }
  4. { Copyright ⌐ 1996, J M Technical Services.  All rights reserved. }
  5.  
  6. interface
  7.  
  8. uses
  9.   WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   StdCtrls;
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     SendBtn: TButton;
  15.     procedure SendBtnClick(Sender: TObject);
  16.   private
  17.     { Private declarations }
  18.   public
  19.     { Public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. { Add SendKey to your uses statement to include the definitions for
  28.   the SK_ constants and the SendKeyString function. You must distribute
  29.   SNDKEYxx.DLL with your application, either in the same directory as
  30.   the executable or in the \windows\system directory. }
  31. uses SendKey;
  32.  
  33. {$R *.DFM}
  34.  
  35. procedure Delay(ms : LongInt);
  36. begin
  37.     ms := GetTickCount + ms;
  38.     while (GetTickCount < ms) do Application.ProcessMessages;
  39. end;
  40.  
  41. procedure TForm1.SendBtnClick(Sender: TObject);
  42. var
  43.     Handle, WHandle: hWnd;
  44.   i: Integer;
  45.   ok: Boolean;
  46. begin
  47.     SendBtn.Enabled := false;
  48.   { Run Notepad }
  49.   if WinExec('NOTEPAD.EXE',SW_SHOW) >= 32 then
  50.   begin
  51.     { Get the handle of the active window (Notepad)
  52.           by waiting until the active window handle changes }
  53.       repeat
  54.          Application.ProcessMessages;
  55.             Handle := FindWindow(PChar(0),'Untitled - Notepad');
  56.     until Handle <> 0;
  57.        { Send the text to the window identified by Handle.
  58.       Use the SK_ constants to insert non-printing characters into the key string.
  59.       These constants have the ASCII values 228 - 255, so the ASCII characters
  60.       represented by these values cannot be included. }
  61.        if SendKeyString(Handle,'This is a demonstration of the SendKeys DLL'+SK_ENTER) then
  62.     { The function returns 'false' if it fails. The trial run version will fail if
  63.         it is run when the Delphi environment is not active. }
  64.     begin
  65.         { Keystrokes can only be sent to the active window which has input focus.
  66.           We pass Handle to the procedure so it can make sure the window is active. }
  67.            ok := SendKeyString(Handle,'for Borland Delphi. It will cost you just $15.00'+SK_ENTER);
  68.            if ok then ok := SendKeyString(Handle,'to register this software.'+SK_ENTER+SK_ENTER);
  69.         { Press the shift key for the whole of the next line to send as capitals. }
  70.            if ok then ok := SendKeyString(Handle,SK_SHIFT_DN+'In this line the Shift key is down'+SK_SHIFT_UP+SK_ENTER+SK_ENTER);
  71.            if ok then ok := SendKeyString(Handle,'Now we will press F1 to look at the help file.'+SK_ENTER);
  72.            if ok then ok := SendKeyString(Handle,SK_F1);
  73.         { WinHelp is a different application, so we must get its handle before
  74.           sending keystrokes. }
  75.         repeat
  76.             Application.ProcessMessages;
  77.                 WHandle := FindWindow(PChar(0),'Help Topics: Notepad Help');
  78.         until (WHandle <> 0);
  79.         { This next sequence will display the help on printing
  80.           in the Win 95 Notepad's help file. }
  81.            if ok then ok := SendKeyString(WHandle,SK_DOWN+SK_DOWN+SK_ENTER+SK_DOWN+SK_ENTER);
  82.       if not ok then exit;
  83.         { Leave the help window for a few seconds so you can see it! }
  84.           Delay(2000);
  85.         { Now close WinHelp. }
  86.         { Note the use of SK_ALT_DN and SK_ALT_UP to represent holding the Alt key down. }
  87.         if ok then ok := SendKeyString(WHandle,SK_ALT_DN+SK_F4+SK_ALT_UP);
  88.         { Wait for WinHelp to close. }
  89.           Delay(1000);
  90.         { Send some more text. }
  91.            if ok then ok := SendKeyString(Handle,SK_ENTER+'That was easy!'+SK_ENTER+SK_ENTER);
  92.            if ok then ok := SendKeyString(Handle,SK_ENTER+'Now lets try a find and replace'+SK_ENTER+SK_ENTER);
  93.       if ok then ok := SendKeyString(Handle,SK_CTRL_DN+SK_HOME+SK_UP);
  94.       if ok then ok := SendKeyString(Handle,SK_ALT_DN+'S'+SK_ALT_UP+'FDelphi'+SK_ENTER);
  95.       if ok then SendMessage(FindWindow(PChar(0),'Find'),WM_CLOSE,0,0);
  96.       if ok then ok := SendKeyString(Handle,SK_CTRL_DN+SK_END+SK_UP);
  97.            if ok then ok := SendKeyString(Handle,'Now save the text and close Notepad.'+SK_ENTER);
  98.         { Send the keystrokes to save the file as TEST.TXT }
  99.            if ok then ok := SendKeyString(Handle,SK_ALT_DN+'FS'+SK_ALT_UP+'TEST.TXT'+SK_ENTER);
  100.         { Possibly the file already exists, so deal with OK to overwrite msg .. }
  101.            WHandle := FindWindow(PChar(0),'Save As');
  102.       if WHandle <> 0 then ok := SendKeyString(WHandle,SK_LEFT+SK_ENTER);
  103.         { .. and close Notepad.  NB: closing an application you are sending keystrokes to }
  104.       { can lock up the sending application, so do it this way ... }
  105.         if ok then SendMessage(Handle,WM_CLOSE,0,0);
  106.     end;
  107.     SendBtn.Enabled := true;
  108.   end;
  109. end;
  110.  
  111. end.
  112.